package com.book.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.book.dto.BookClassesDto;
import com.book.entity.BookClasses;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface BookClassesDao extends BaseMapper<BookClasses> {
    //这里是获取这本书所有的类名，是字符串数组
    @Select("select class_name from book_class a,book_classes b \n" +
            "where a.id=b.class_id and b.book_id=#{id} and b.del_flag=0 and a.del_flag=0")
    String[] getClasses(@Param("id") int id);

    //这里是获取这本书所有的类名的id，是整型数组，管理员才用得到
    @Select("select b.class_id from book_class a,book_classes b \n" +
            "where a.id=b.class_id and b.book_id=#{id} and b.del_flag=0 and a.del_flag=0")
    Integer[] getBookClassesById(@Param("id") int id);

    @Select("SELECT \n" +
            "   b.id as id,b.book_id as bookId,a.id as classId,a.class_name as className\n" +
            "FROM book_class a,book_classes b \n" +
            "where\n" +
            "   a.id=b.class_id and \n" +
            "   (\n" +
            "       (0!=#{bookId} and b.book_id=#{bookId}) or \n" +
            "       (0=#{bookId})\n" +
            "   )\n" +
            "   and a.del_flag=0 and b.del_flag=0\n"+
            "order by b.book_id desc,b.id desc"
            )
    List<BookClassesDto> getBookClasses(@Param("bookId") int bookId);
}